package com.bw.mvvm_comom.view;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_comom.filed.UIFiled;
import com.bw.mvvm_comom.mvvm.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

public abstract class BaseActivity<V extends ViewDataBinding,VM extends BaseViewModel> extends AppCompatActivity implements IView {

    protected  V v;
    protected VM vm;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getUiChange().getHideDialogObservable().observe(this,hideDialogObServer);
        vm.getUiChange().getFinishActObServAble().observe(this,finishDialogObServer);
        vm.getUiChange().getShowDialogObservable().observe(this,showDialogObServer);
        vm.getUiChange().getShowMsgObservable().observe(this,showMsgObServer);
        vm.getUiChange().getStartPageObServAble().observe(this,startPageObServer);
        bindDataBinding();
        v.setVariable(initVerable(),vm);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        vm.getUiChange().getHideDialogObservable().removeObserver(hideDialogObServer);
        vm.getUiChange().getFinishActObServAble().removeObserver(finishDialogObServer);
        vm.getUiChange().getShowDialogObservable().removeObserver(showDialogObServer);
        vm.getUiChange().getShowMsgObservable().removeObserver(showMsgObServer);
        vm.getUiChange().getStartPageObServAble().removeObserver(startPageObServer);
        getLifecycle().removeObserver(vm);
        v.unbind();
    }

    private Observer<String> showDialogObServer = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            showDialog();
        }

    };


    private Observer<String> hideDialogObServer = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            hideDialog();
        }

    };

    private Observer<String> finishDialogObServer = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            finishPage();
        }

    };

    private Observer<String> showMsgObServer = new Observer<String>() {
        @Override
        public void onChanged(String o) {
            showMsg(o);
        }

    };

    private Observer<HashMap<String,Object>> startPageObServer = new Observer<HashMap<String,Object>>() {
        @Override
        public void onChanged(HashMap<String, Object> map) {
            Class clazz = (Class) map.get(UIFiled.PAGECLAZZ);
            Object o = map.get(UIFiled.PAGEBUNDLE);
            Bundle bundle = null;
            if (o != null)
                bundle = (Bundle) o;
                startPage(clazz,bundle);
        }
    };

    @Override
    public void showMsg(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void finishPage() {
        finish();
    }

    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(this,clazz);
        if (bundle != null){
            intent.putExtra(UIFiled.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }

    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm = (VM) new ViewModelProvider(this,
                new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }

    @Override
    public void bindDataBinding() {
        v = DataBindingUtil.setContentView(this,bindLayout());
    }


}
